package com.app.biz.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.app.common.annotation.Anonymous;
import com.app.common.config.AppConfig;
import com.app.common.core.page.TableDataInfo;
import com.app.common.utils.file.FileUploadUtils;
import com.app.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.app.common.annotation.Log;
import com.app.common.core.controller.BaseController;
import com.app.common.core.domain.AjaxResult;
import com.app.common.enums.BusinessType;
import com.app.biz.domain.BizProduceSale;
import com.app.biz.service.IBizProduceSaleService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 重点地区产销率完成情况Controller
 * 
 * @author Zhou_S
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/biz/sale")
public class BizProduceSaleController extends BaseController
{
    @Autowired
    private IBizProduceSaleService bizProduceSaleService;

    /**
     * 查询重点地区产销率完成情况列表
     */
    @PreAuthorize("@ss.hasPermi('biz:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizProduceSale bizProduceSale)
    {
        if(StringUtils.isNotBlank(bizProduceSale.getDates())){
            DateTime datesTimes= DateUtil.parse(bizProduceSale.getDates(),"yyyy年MM月");
            bizProduceSale.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        }else{
            AjaxResult.warn("未指定日期！");
        }
        startPage();
        List<BizProduceSale> list = bizProduceSaleService.selectBizProduceSaleList(bizProduceSale);
        return getDataTable(list);
    }

    /**
     * 导出重点地区产销率完成情况列表
     */
    @PreAuthorize("@ss.hasPermi('biz:sale:export')")
    @Log(title = "重点地区产销率完成情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProduceSale bizProduceSale)
    {
        List<BizProduceSale> list = bizProduceSaleService.selectBizProduceSaleList(bizProduceSale);
        ExcelUtil<BizProduceSale> util = new ExcelUtil<BizProduceSale>(BizProduceSale.class);
        util.exportExcel(response, list, "重点地区产销率完成情况数据");
    }

    /**
     * 获取重点地区产销率完成情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:sale:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizProduceSaleService.selectBizProduceSaleByBaseId(baseId));
    }

    /**
     * 新增重点地区产销率完成情况
     */
    @PreAuthorize("@ss.hasPermi('biz:sale:add')")
    @Log(title = "重点地区产销率完成情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProduceSale bizProduceSale)
    {
        return toAjax(bizProduceSaleService.insertBizProduceSale(bizProduceSale));
    }

    /**
     * 修改重点地区产销率完成情况
     */
    @PreAuthorize("@ss.hasPermi('biz:sale:edit')")
    @Log(title = "重点地区产销率完成情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProduceSale bizProduceSale)
    {
        return toAjax(bizProduceSaleService.updateBizProduceSale(bizProduceSale));
    }

    /**
     * 删除重点地区产销率完成情况
     */
    @PreAuthorize("@ss.hasPermi('biz:sale:remove')")
    @Log(title = "重点地区产销率完成情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizProduceSaleService.deleteBizProduceSaleByBaseIds(baseIds));
    }

    /**
     * <p>
     *     查询APP左上角的日期数据
     * </p>
     * @return 日期的集合
     */
    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizProduceSaleService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }
    /**
     * <p>
     *     清空所有的重点地区产销率完成情况数据
     * </p>
     * @return
     */
    @DeleteMapping("/clearAllTableData")
    public AjaxResult clearAllTableData(){
        bizProduceSaleService.clearAllTableData();
        return AjaxResult.success();
    }

    /**
     * <p>
     *     将临时表中的数据迁移到正式表虽
     * </p>
     * @return
     */
    @PostMapping("/transferDataInOneClick")
    public AjaxResult transferDataInOneClick(){
        return toAjax(bizProduceSaleService.transferDataInOneClick());
    }

    /**
     * <p>
     *     导入重点地区产销率数据
     * </p>
     * @param params 前端传来的参数，目前只有一个月份
     * @param multipartFile 上传的Excel文件
     * @return 导入是否成功
     */
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam Map<String,String> params, @RequestPart("file") MultipartFile multipartFile){
        try{
            String yearMonth = params.get("yearMonth");
            if(StringUtils.isBlank(yearMonth)){
                return AjaxResult.warn("请选择导入的年月");
            }
            List<BizProduceSale> list = bizProduceSaleService.selectBizProduceSaleListNoParams();
            if(list != null && !list.isEmpty()){
                return AjaxResult.warn("导入之前请先清除历史数据");
            }
            /* 上传文件的路径 */
            String uploadPath = FileUploadUtils.upload(AppConfig.getProfile(),multipartFile);
            String absoluteExcelPath = FileUploadUtils.replaceAbsolutePath(uploadPath);
            bizProduceSaleService.importExcel(absoluteExcelPath,yearMonth);
            return AjaxResult.success();
        }catch (IOException e){
            return AjaxResult.error("导入失败");
        }
    }
}
